package com.guoliang.orderService.controller;

import com.guoliang.commonUtils.CommonResult;
import com.guoliang.orderService.service.PayLogService;
import com.guoliang.service.exceptionHandler.MyException;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.runtime.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author leo
 * @since 2023-01-31
 */
@RestController
@RequestMapping("/orderService/payLog")
public class PayLogController {
    @Autowired
    private PayLogService payService;

    @ApiOperation("生成二维码")
    @GetMapping("createNative/{orderNo}")
    public CommonResult createNative(@PathVariable String orderNo) {
        Map map = payService.createNative(orderNo);
        return CommonResult.ok().date(map);
    }
    @ApiOperation("查询订单状态")
    @GetMapping("getOrderStatus/{orderNo}")
    public CommonResult getOrderStatus(@PathVariable String orderNo){
        System.out.println("***********"+orderNo);
        //查询订单状态
        Map<String,String> map=payService.getOrderStatus(orderNo);

        if (map==null){
            throw new MyException(20001,"支付失败");
        }
        if(map.get("trade_state").equals("SUCCESS")){
            //更新订单状态 添加到支付表
            payService.updateOrderStatus(map);
            return CommonResult.ok().message("支付成功");
        }
        return CommonResult.ok().code(25000).message("正在支付");
    }
}
